<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 5.4.2">
  <link rel="apple-touch-icon" sizes="180x180" href="/blog/images/icon.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/blog/images/icon.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/blog/images/icon.png">
  <link rel="mask-icon" href="/blog/images/icon.svg" color="#222">

<link rel="stylesheet" href="/blog/css/main.css">


<link rel="stylesheet" href="/blog/lib/font-awesome/css/all.min.css">

<script id="hexo-configurations">
    var NexT = window.NexT || {};
    var CONFIG = {"hostname":"bgape002.gitee.io","root":"/blog/","scheme":"Gemini","version":"7.8.0","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12,"onmobile":false},"copycode":{"enable":true,"show_result":true,"style":"mac"},"back2top":{"enable":true,"sidebar":false,"scrollpercent":true},"bookmark":{"enable":false,"color":"#222","save":"auto"},"fancybox":false,"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"algolia":{"hits":{"per_page":10},"labels":{"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}},"localsearch":{"enable":true,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false},"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},"path":"search.xml"};
  </script>

  <meta name="description" content="HelloWorld1234567#include &lt;iostream&gt;using namespace std;int main()&amp;#123;    cout &lt;&lt; &quot;Hello World.&quot;;    return 0;&amp;#125;  注释 &#x2F;&#x2F; 单行注释 &#x2F;*...*&#x2F;多行注释  常量 #define 常量名 常量值：定义宏常量 const 数据类">
<meta property="og:type" content="article">
<meta property="og:title" content="C++">
<meta property="og:url" content="https://bgape002.gitee.io/2021/09/05/C++/index.html">
<meta property="og:site_name" content="bgape002">
<meta property="og:description" content="HelloWorld1234567#include &lt;iostream&gt;using namespace std;int main()&amp;#123;    cout &lt;&lt; &quot;Hello World.&quot;;    return 0;&amp;#125;  注释 &#x2F;&#x2F; 单行注释 &#x2F;*...*&#x2F;多行注释  常量 #define 常量名 常量值：定义宏常量 const 数据类">
<meta property="og:locale" content="zh_CN">
<meta property="article:published_time" content="2021-09-04T16:00:00.000Z">
<meta property="article:modified_time" content="2022-07-05T01:32:07.485Z">
<meta property="article:author" content="bgape002">
<meta property="article:tag" content="C++">
<meta name="twitter:card" content="summary">

<link rel="canonical" href="https://bgape002.gitee.io/2021/09/05/C++/">


<script id="page-configurations">
  // https://hexo.io/docs/variables.html
  CONFIG.page = {
    sidebar: "",
    isHome : false,
    isPost : true,
    lang   : 'zh-CN'
  };
</script>

  <title>C++ | bgape002</title>
  






  <noscript>
  <style>
  .use-motion .brand,
  .use-motion .menu-item,
  .sidebar-inner,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-header { opacity: initial; }

  .use-motion .site-title,
  .use-motion .site-subtitle {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line-before i { left: initial; }
  .use-motion .logo-line-after i { right: initial; }
  </style>
</noscript>

</head>

<body itemscope itemtype="http://schema.org/WebPage">
  <div class="container use-motion">
    <div class="headband"></div>

    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏">
      <span class="toggle-line toggle-line-first"></span>
      <span class="toggle-line toggle-line-middle"></span>
      <span class="toggle-line toggle-line-last"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/blog/" class="brand" rel="start">
      <span class="logo-line-before"><i></i></span>
      <h1 class="site-title">bgape002</h1>
      <span class="logo-line-after"><i></i></span>
    </a>
      <p class="site-subtitle" itemprop="description">淡泊明志，宁静致远</p>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
        <i class="fa fa-search fa-fw fa-lg"></i>
    </div>
  </div>
</div>




<nav class="site-nav">
  <ul id="menu" class="main-menu menu">
        <li class="menu-item menu-item-home">

    <a href="/blog/" rel="section"><i class="fa fa-home fa-fw"></i>首页</a>

  </li>
        <li class="menu-item menu-item-tags">

    <a href="/blog/tags/" rel="section"><i class="fa fa-tags fa-fw"></i>标签<span class="badge">66</span></a>

  </li>
        <li class="menu-item menu-item-categories">

    <a href="/blog/categories/" rel="section"><i class="fa fa-th fa-fw"></i>分类<span class="badge">27</span></a>

  </li>
        <li class="menu-item menu-item-archives">

    <a href="/blog/archives/" rel="section"><i class="fa fa-archive fa-fw"></i>归档<span class="badge">61</span></a>

  </li>
      <li class="menu-item menu-item-search">
        <a role="button" class="popup-trigger"><i class="fa fa-search fa-fw"></i>搜索
        </a>
      </li>
  </ul>
</nav>



  <div class="search-pop-overlay">
    <div class="popup search-popup">
        <div class="search-header">
  <span class="search-icon">
    <i class="fa fa-search"></i>
  </span>
  <div class="search-input-container">
    <input autocomplete="off" autocapitalize="off"
           placeholder="搜索..." spellcheck="false"
           type="search" class="search-input">
  </div>
  <span class="popup-btn-close">
    <i class="fa fa-times-circle"></i>
  </span>
</div>
<div id="search-result">
  <div id="no-result">
    <i class="fa fa-spinner fa-pulse fa-5x fa-fw"></i>
  </div>
</div>

    </div>
  </div>

</div>
    </header>

    
  <div class="back-to-top">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>


    <main class="main">
      <div class="main-inner">
        <div class="content-wrap">
          

          <div class="content post posts-expand">
            

    
  
  
  <article itemscope itemtype="http://schema.org/Article" class="post-block" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="https://bgape002.gitee.io/2021/09/05/C++/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/blog/images/head.png">
      <meta itemprop="name" content="bgape002">
      <meta itemprop="description" content="mail: bgape002@163.com">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="bgape002">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          C++
        </h1>

        <div class="post-meta">
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-calendar"></i>
              </span>
              <span class="post-meta-item-text">发表于</span>

              <time title="创建时间：2021-09-05 00:00:00" itemprop="dateCreated datePublished" datetime="2021-09-05T00:00:00+08:00">2021-09-05</time>
            </span>
              <span class="post-meta-item">
                <span class="post-meta-item-icon">
                  <i class="far fa-calendar-check"></i>
                </span>
                <span class="post-meta-item-text">更新于</span>
                <time title="修改时间：2022-07-05 09:32:07" itemprop="dateModified" datetime="2022-07-05T09:32:07+08:00">2022-07-05</time>
              </span>
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-folder"></i>
              </span>
              <span class="post-meta-item-text">分类于</span>
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/blog/categories/C/" itemprop="url" rel="index"><span itemprop="name">C</span></a>
                </span>
            </span>

          
            <span class="post-meta-item" title="阅读次数" id="busuanzi_container_page_pv" style="display: none;">
              <span class="post-meta-item-icon">
                <i class="fa fa-eye"></i>
              </span>
              <span class="post-meta-item-text">阅读次数：</span>
              <span id="busuanzi_value_page_pv"></span>
            </span>
  
  <span class="post-meta-item">
    
      <span class="post-meta-item-icon">
        <i class="far fa-comment"></i>
      </span>
      <span class="post-meta-item-text">Valine：</span>
    
    <a title="valine" href="/blog/2021/09/05/C++/#valine-comments" itemprop="discussionUrl">
      <span class="post-comments-count valine-comment-count" data-xid="/blog/2021/09/05/C++/" itemprop="commentCount"></span>
    </a>
  </span>
  
  

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">

      
        <h3 id="HelloWorld"><a href="#HelloWorld" class="headerlink" title="HelloWorld"></a>HelloWorld</h3><figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;iostream&gt;</span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> std;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="type">int</span> <span class="title">main</span><span class="params">()</span></span>&#123;</span><br><span class="line">    cout &lt;&lt; <span class="string">&quot;Hello World.&quot;</span>;</span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="注释"><a href="#注释" class="headerlink" title="注释"></a>注释</h3><ul>
<li>// 单行注释</li>
<li><code>/*...*/</code>多行注释</li>
</ul>
<h3 id="常量"><a href="#常量" class="headerlink" title="常量"></a>常量</h3><ul>
<li><code>#define 常量名 常量值</code>：定义宏常量</li>
<li><code>const 数据类型 常量名=常量值</code>:静态变量</li>
</ul>
<h3 id="关键字（保留字）"><a href="#关键字（保留字）" class="headerlink" title="关键字（保留字）"></a>关键字（保留字）</h3><table>
<thead>
<tr>
<th>asm</th>
<th>do</th>
<th>if</th>
<th>return</th>
<th>typedef</th>
</tr>
</thead>
<tbody><tr>
<td>auto</td>
<td>double</td>
<td>inline</td>
<td>short</td>
<td>typeid</td>
</tr>
<tr>
<td>bool</td>
<td>dynamic_cast</td>
<td>int</td>
<td>signed</td>
<td>typename</td>
</tr>
<tr>
<td>break</td>
<td>else</td>
<td>long</td>
<td>sizeof</td>
<td>union</td>
</tr>
<tr>
<td>case</td>
<td>enum</td>
<td>mutable</td>
<td>static</td>
<td>unsigned</td>
</tr>
<tr>
<td>catch</td>
<td>explicit</td>
<td>namespace</td>
<td>static_cast</td>
<td>using</td>
</tr>
<tr>
<td>char</td>
<td>export</td>
<td>new</td>
<td>struct</td>
<td>virtual</td>
</tr>
<tr>
<td>class</td>
<td>extern</td>
<td>operator</td>
<td>switch</td>
<td>void</td>
</tr>
<tr>
<td>const</td>
<td>false</td>
<td>private</td>
<td>template</td>
<td>volatile</td>
</tr>
<tr>
<td>const_cast</td>
<td>float</td>
<td>protected</td>
<td>this</td>
<td>wchar_t</td>
</tr>
<tr>
<td>continue</td>
<td>for</td>
<td>public</td>
<td>throw</td>
<td>while</td>
</tr>
<tr>
<td>default</td>
<td>friend</td>
<td>register</td>
<td>true</td>
<td></td>
</tr>
<tr>
<td>delete</td>
<td>goto</td>
<td>reinterpret_cast</td>
<td>try</td>
<td></td>
</tr>
</tbody></table>
<span id="more"></span>

<h3 id="标识符命名规则"><a href="#标识符命名规则" class="headerlink" title="标识符命名规则"></a>标识符命名规则</h3><ul>
<li>标识符不能是关键字</li>
<li>标识符只能由字母、数字、下划线</li>
<li>第一个字符不能为数字</li>
<li>标识符中字母区分大小写</li>
</ul>
<h3 id="数据类型"><a href="#数据类型" class="headerlink" title="数据类型"></a>数据类型</h3><p>使用编程语言进行编程时，需要用到各种变量来存储各种信息。变量保留的是它所存储的值的内存位置。 </p>
<h3 id="基本数据类型"><a href="#基本数据类型" class="headerlink" title="基本数据类型"></a>基本数据类型</h3><table>
<thead>
<tr>
<th align="left">类型</th>
<th align="left">关键字</th>
</tr>
</thead>
<tbody><tr>
<td align="left">布尔型</td>
<td align="left">bool</td>
</tr>
<tr>
<td align="left">字符型</td>
<td align="left">char</td>
</tr>
<tr>
<td align="left">整型</td>
<td align="left">int</td>
</tr>
<tr>
<td align="left">浮点型</td>
<td align="left">float</td>
</tr>
<tr>
<td align="left">双浮点型</td>
<td align="left">double</td>
</tr>
<tr>
<td align="left">无类型</td>
<td align="left">void</td>
</tr>
<tr>
<td align="left">宽字符型</td>
<td align="left">wchar_t</td>
</tr>
</tbody></table>
<h3 id="各数据类型占用空间"><a href="#各数据类型占用空间" class="headerlink" title="各数据类型占用空间"></a>各数据类型占用空间</h3><table>
<thead>
<tr>
<th align="left">类型</th>
<th align="left">位</th>
<th align="left">范围</th>
</tr>
</thead>
<tbody><tr>
<td align="left">char</td>
<td align="left">1 个字节</td>
<td align="left">-128 到 127 或者 0 到 255</td>
</tr>
<tr>
<td align="left">unsigned char</td>
<td align="left">1 个字节</td>
<td align="left">0 到 255</td>
</tr>
<tr>
<td align="left">signed char</td>
<td align="left">1 个字节</td>
<td align="left">-128 到 127</td>
</tr>
<tr>
<td align="left">int</td>
<td align="left">4 个字节</td>
<td align="left">-2147483648 到 2147483647</td>
</tr>
<tr>
<td align="left">unsigned int</td>
<td align="left">4 个字节</td>
<td align="left">0 到 4294967295</td>
</tr>
<tr>
<td align="left">signed int</td>
<td align="left">4 个字节</td>
<td align="left">-2147483648 到 2147483647</td>
</tr>
<tr>
<td align="left">short int</td>
<td align="left">2 个字节</td>
<td align="left">-32768 到 32767</td>
</tr>
<tr>
<td align="left">unsigned short int</td>
<td align="left">2 个字节</td>
<td align="left">0 到 65,535</td>
</tr>
<tr>
<td align="left">signed short int</td>
<td align="left">2 个字节</td>
<td align="left">-32768 到 32767</td>
</tr>
<tr>
<td align="left">long int</td>
<td align="left">8 个字节</td>
<td align="left">-9,223,372,036,854,775,808 到 9,223,372,036,854,775,807</td>
</tr>
<tr>
<td align="left">signed long int</td>
<td align="left">8 个字节</td>
<td align="left">-9,223,372,036,854,775,808 到 9,223,372,036,854,775,807</td>
</tr>
<tr>
<td align="left">unsigned long int</td>
<td align="left">8 个字节</td>
<td align="left">0 到 18,446,744,073,709,551,615</td>
</tr>
<tr>
<td align="left">float</td>
<td align="left">4 个字节</td>
<td align="left">精度型占4个字节（32位）内存空间，+/- 3.4e +/- 38 (~7 个数字)</td>
</tr>
<tr>
<td align="left">double</td>
<td align="left">8 个字节</td>
<td align="left">双精度型占8 个字节（64位）内存空间，+/- 1.7e +/- 308 (~15 个数字)</td>
</tr>
<tr>
<td align="left">long double</td>
<td align="left">16 个字节</td>
<td align="left">长双精度型 16 个字节（128位）内存空间，可提供18-19位有效数字。</td>
</tr>
<tr>
<td align="left">wchar_t</td>
<td align="left">2 或 4 个字节</td>
<td align="left">1 个宽字符</td>
</tr>
</tbody></table>
<h3 id="sizeof、typedf、enum"><a href="#sizeof、typedf、enum" class="headerlink" title="sizeof、typedf、enum"></a>sizeof、typedf、enum</h3><ul>
<li><p><code>sizeof(数据类型/变量)</code>：统计占用内存大小</p>
</li>
<li><p><code>typedf</code>：为一个已有的类型取一个新名称。</p>
<blockquote>
<p><code>typedf type newname;</code></p>
<p><code>typedf int feet;</code></p>
</blockquote>
</li>
<li><p><code>enum</code>:枚举类型，将变量值一一例举。 如果一个变量只有几种可能的值，可以定义为枚举(enumeration)类型。所谓”枚举”是指将变量的值一一列举出来，<strong>变量的值只能在列举出来的值的范围内</strong>。 </p>
<blockquote>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">enum</span> 枚举名&#123; </span><br><span class="line">  标识符[=整型常数], </span><br><span class="line">  标识符[=整型常数], </span><br><span class="line">... </span><br><span class="line"> 标识符[=整型常数]</span><br><span class="line">&#125; 枚举变量;</span><br><span class="line"></span><br><span class="line"><span class="keyword">enum</span> <span class="title class_">color</span> &#123; red, green, blue &#125; c;</span><br><span class="line">c = blue;</span><br></pre></td></tr></table></figure></blockquote>
</li>
</ul>
<h3 id="字符与字符串"><a href="#字符与字符串" class="headerlink" title="字符与字符串"></a>字符与字符串</h3><ul>
<li><p><code>char</code>：用于表示单个字符</p>
<blockquote>
<ul>
<li>在显示字符型变量时，用单引号将字符括起来，不用双引号</li>
<li>字符型变量占用1个字节</li>
<li>字符型变量存储时，是将对应的ascll码放进存储单元</li>
</ul>
</blockquote>
</li>
<li><p>字符串，用于表示一串字符</p>
<blockquote>
<ol>
<li><code>char str[] = &quot;bgape002&quot;</code></li>
<li><code>string str = &quot;bgape002&quot;</code></li>
</ol>
</blockquote>
</li>
<li><p>C++单双引号的区别</p>
<blockquote>
<p><strong>单引号是字符型；双引号是字符串型</strong></p>
<p>单引号引起的一个字符实际上代表一个<strong>整数</strong>。</p>
<p>双引号引起的字符串，代表的却是一个指向无名<strong>数组</strong>起始字符的指针。该数组会被双引号之间的字符以及一个额外的二进制为零的字符 ‘\0’ 初始化。</p>
</blockquote>
</li>
</ul>
<h3 id="转义字符"><a href="#转义字符" class="headerlink" title="转义字符"></a>转义字符</h3><p>用于表示一些不能显示出来的ascll字符</p>
<table>
<thead>
<tr>
<th align="center">转义字符形式</th>
<th>含义</th>
<th align="center">ASCII码值</th>
</tr>
</thead>
<tbody><tr>
<td align="center"><code>\a</code></td>
<td>响铃符</td>
<td align="center">7</td>
</tr>
<tr>
<td align="center"><code>\b</code></td>
<td>退格符，将光标位置移到下一页开头</td>
<td align="center">8</td>
</tr>
<tr>
<td align="center"><code>\f</code></td>
<td>进纸符，将光标位置移到下一页开头</td>
<td align="center">12</td>
</tr>
<tr>
<td align="center"><code>\n</code></td>
<td>换行符，将光标位置移到下一行开头</td>
<td align="center">10</td>
</tr>
<tr>
<td align="center"><code>\r</code></td>
<td>回车符，将光标位置移到本行开头</td>
<td align="center">13</td>
</tr>
<tr>
<td align="center"><code>\t</code></td>
<td>水平制表符，光标跳到下一个Tab位置</td>
<td align="center">9</td>
</tr>
<tr>
<td align="center"><code>\v</code></td>
<td>垂直制表符</td>
<td align="center">11</td>
</tr>
<tr>
<td align="center"><code>\&#39;</code></td>
<td>单引号</td>
<td align="center">39</td>
</tr>
<tr>
<td align="center"><code>\&quot;</code></td>
<td>双引号</td>
<td align="center">34</td>
</tr>
<tr>
<td align="center">``</td>
<td>单反斜杠</td>
<td align="center">92</td>
</tr>
<tr>
<td align="center"><code>\?</code></td>
<td>问号</td>
<td align="center">63</td>
</tr>
<tr>
<td align="center"><code>\0</code></td>
<td>空字符</td>
<td align="center">0</td>
</tr>
<tr>
<td align="center"><code>\ooo</code></td>
<td>用1~3位八进制数 ooo 为码值所对应的字符</td>
<td align="center">ooo（八进制）</td>
</tr>
<tr>
<td align="center"><code>\xhh</code></td>
<td>用1、2位十六进制 hh 为码符所对应的字符</td>
<td align="center">hh（十六进制）</td>
</tr>
</tbody></table>
<h3 id="数据的输入cin"><a href="#数据的输入cin" class="headerlink" title="数据的输入cin"></a>数据的输入<code>cin</code></h3><p>从键盘上获取数据。</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;iostream&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;string&gt;</span></span></span><br><span class="line"></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> std;</span><br><span class="line"><span class="function"><span class="type">int</span> <span class="title">main</span><span class="params">()</span></span>&#123;</span><br><span class="line">    string str = <span class="string">&quot;bgape002&quot;</span>;</span><br><span class="line">    cout &lt;&lt; <span class="string">&quot;请输入一个字符串：&quot;</span> &lt;&lt; endl;</span><br><span class="line">    cin &gt;&gt; str;</span><br><span class="line">    cout &lt;&lt; <span class="string">&quot;字符串str=&quot;</span> &lt;&lt; str &lt;&lt; endl;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="三目运算符"><a href="#三目运算符" class="headerlink" title="三目运算符"></a>三目运算符</h3><p>语法：<code>表达式1?:表达式2:表达式3</code>；表达式1为真，则执行表达式2，否则执行表达式3。</p>
<h3 id="类型转换"><a href="#类型转换" class="headerlink" title="类型转换"></a>类型转换</h3><ul>
<li>隐式转换：<code>int a=10;double b=(double) a;</code></li>
<li>static_cast:1.无运行时安全检测；2.不能交叉转换，非同一继承体系无法转换；3.将非<code>const</code>转换成<code>const</code><ul>
<li><code>int a=10;double b=static_cast&lt;double&gt;(a);</code></li>
</ul>
</li>
<li>dynamic_cast:一般用于多态类型的转换，运行时会进行安全检测（不安全转换会将地址置为<code>NULL</code>）</li>
<li>reinterpret_cast:底层强制转换，不做类型检测，直接拷贝二进制数据。int -&gt; double 二进制拷贝后，会按浮点数进行显示值，也即数值会变化。</li>
<li>const_cast:去除<code>const</code>属性，将<code>const</code>转换成非<code>const</code></li>
</ul>
<h3 id="函数的分文件编写"><a href="#函数的分文件编写" class="headerlink" title="函数的分文件编写"></a>函数的分文件编写</h3><ul>
<li>创建.h后缀名的头文件</li>
<li>创建.cpp后缀名的源文件</li>
<li>在头文件中写函数的声明</li>
<li>在源文件中写函数的定义，引入<code>#include &quot;xx.h&quot;</code>进行头文件关联</li>
<li>调用：<code>#include &quot;xx.h&quot;</code></li>
</ul>
<h3 id="指针"><a href="#指针" class="headerlink" title="指针"></a>指针</h3><ul>
<li><p>指针的作用：可以通过指针间接访问内存</p>
</li>
<li><p>指针变量的定义：<code>数据类型 *变量名；</code>,<code>int a=10;int *p;p=&amp;a;</code></p>
</li>
<li><p>占用内容：32为系统下占用4个字节，64位系统占用8个字节</p>
</li>
<li><p>指针常量：指针指向的值可以改，指针指向不可以改，<code>int * const p = &amp;a;</code></p>
</li>
<li><p>常量指针：指针指向可以修改，指针指向值不可以改，<code>const int *p = $a;</code></p>
</li>
</ul>
<h3 id="结构体"><a href="#结构体" class="headerlink" title="结构体"></a>结构体</h3><p>结构体属于用户自定义的数据类型，允许用户存储不同的数据类型</p>
<ul>
<li><p>结构体的定义：<code>struct 结构体名 &#123;结构体成员列表&#125;;</code></p>
<blockquote>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;iostream&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;string&gt;</span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> std;</span><br><span class="line"><span class="comment">// 定义方式1</span></span><br><span class="line"><span class="keyword">struct</span> <span class="title class_">student</span>&#123;</span><br><span class="line"> string name;</span><br><span class="line"> <span class="type">int</span> age;</span><br><span class="line"> <span class="type">int</span> score;</span><br><span class="line">&#125;;</span><br><span class="line"><span class="function"><span class="type">int</span> <span class="title">main</span><span class="params">()</span></span>&#123;</span><br><span class="line"> <span class="keyword">struct</span> <span class="title class_">student</span> s1;</span><br><span class="line"> s1.name = <span class="string">&quot;bgape002&quot;</span>;</span><br><span class="line"> s1.age = <span class="number">10</span>;</span><br><span class="line"> s1.score = <span class="number">99</span>;</span><br><span class="line"> cout &lt;&lt; s1.name;</span><br><span class="line"> retrun <span class="number">0</span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">// 定义方式2：外部定义时创建结构体变量</span></span><br><span class="line"><span class="keyword">struct</span> <span class="title class_">student</span>&#123;</span><br><span class="line"> string name;</span><br><span class="line"> <span class="type">int</span> age;</span><br><span class="line"> <span class="type">int</span> score;</span><br><span class="line">&#125;s2;</span><br><span class="line"><span class="comment">// 定义方式3:外部声明，内部赋值</span></span><br><span class="line"><span class="keyword">struct</span> <span class="title class_">student</span> s3 = &#123;<span class="string">&quot;bgape002&quot;</span>,<span class="number">18</span>,<span class="number">99</span>&#125;;</span><br></pre></td></tr></table></figure></blockquote>
</li>
<li><p>结构体指针,<code>-&gt;</code>访问成员属性</p>
<blockquote>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;iostream&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;string&gt;</span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> std;</span><br><span class="line"><span class="comment">// 定义方式1</span></span><br><span class="line"><span class="keyword">struct</span> <span class="title class_">student</span>&#123;</span><br><span class="line"> string name;</span><br><span class="line"> <span class="type">int</span> age;</span><br><span class="line"> <span class="type">int</span> score;</span><br><span class="line">&#125;;</span><br><span class="line"><span class="function"><span class="type">int</span> <span class="title">main</span><span class="params">()</span></span>&#123;</span><br><span class="line"> <span class="keyword">struct</span> <span class="title class_">student</span> s1;</span><br><span class="line"> s1.name = <span class="string">&quot;bgape002&quot;</span>;</span><br><span class="line"> s1.age = <span class="number">10</span>;</span><br><span class="line"> s1.score = <span class="number">99</span>;</span><br><span class="line"> <span class="keyword">struct</span> <span class="title class_">student</span> *p = &amp;s1;</span><br><span class="line"> cout &lt;&lt; p-&gt;name;</span><br><span class="line"> </span><br><span class="line"> retrun <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></blockquote>
</li>
</ul>
<h3 id="内存分区模型"><a href="#内存分区模型" class="headerlink" title="内存分区模型"></a>内存分区模型</h3><ul>
<li><p>代码区：存放函数体的二进制代码，由操作系统进行管理</p>
<ul>
<li>存放cpu执行的机器指令</li>
<li>代码区是共享的，对于频繁被执行的程序，内存中有一份代码即可</li>
<li>代码区是只读的，防止程序意外地修改了代码</li>
</ul>
</li>
<li><p>全局区：存放全局变量、静态变量和常量</p>
<ul>
<li>该区域的数据在程序结束后由操作系统释放</li>
</ul>
</li>
<li><p>栈区：由编译器自动分配释放，存放函数的参数值，局部变量等</p>
<ul>
<li><strong>注意：</strong>不要返回局部变量的地址，栈区开辟的数据由编译器自动释放</li>
</ul>
</li>
<li><p>堆区：由程序员分配和释放，若程序员不释放，程序结束时由操作系统回收</p>
<ul>
<li>c++中主要利用new在带堆区开辟内存</li>
</ul>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;iostream&gt;</span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> std;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="type">int</span> *<span class="title">func</span><span class="params">()</span></span>&#123;</span><br><span class="line">    <span class="type">int</span> *p = <span class="keyword">new</span> <span class="built_in">int</span>(<span class="number">10</span>);</span><br><span class="line">    <span class="keyword">return</span> p;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="type">int</span> <span class="title">main</span><span class="params">()</span></span>&#123;</span><br><span class="line">    <span class="type">int</span> *p = <span class="built_in">func</span>();</span><br><span class="line">    cout &lt;&lt; *p &lt;&lt; endl;</span><br><span class="line">    cout &lt;&lt; *p &lt;&lt; endl;</span><br><span class="line">    <span class="keyword">delete</span> p; <span class="comment">// 释放数组时，用delete [] p;</span></span><br><span class="line">    cout &lt;&lt; *p &lt;&lt; endl;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></li>
</ul>
<p>不同区域存放数据，赋予不同的生命周期，提升编程的灵活性。</p>
<h3 id="引用"><a href="#引用" class="headerlink" title="引用"></a>引用</h3><p>给变量起别名;<strong>引用的本质在c++内部实现是一个指针常量</strong></p>
<ul>
<li><p>语法：<code>数据类型 &amp;别名 = 原名;</code></p>
</li>
<li><p>引用必须要初始化，且初始化后就不可更改了</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 交换值</span></span><br><span class="line"><span class="comment">// 1.地址传递</span></span><br><span class="line"><span class="function"><span class="type">void</span> <span class="title">swapab1</span><span class="params">(<span class="type">int</span> *a, <span class="type">int</span> *b)</span></span>&#123;</span><br><span class="line">    <span class="type">int</span> temp = *a;</span><br><span class="line">    *a = *b;</span><br><span class="line">    *b = temp;</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">// 2.引用传递</span></span><br><span class="line"><span class="function"><span class="type">void</span> <span class="title">swapab2</span><span class="params">(<span class="type">int</span> &amp;a, <span class="type">int</span> &amp;b)</span></span>&#123;</span><br><span class="line">    <span class="type">int</span> temp = a;</span><br><span class="line">    a = b;</span><br><span class="line">    b = temp;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></li>
</ul>
<p>c++面向对象的三大特性：<strong>封装、继承、多态</strong></p>
<h3 id="封装"><a href="#封装" class="headerlink" title="封装"></a>封装</h3><ul>
<li><p>将属性和行为作为一个整体，表现生活中的事物</p>
<blockquote>
<p>语法：<code>class 类名｛ 访问权限：属性/行为｝;</code></p>
</blockquote>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 设计一个圆类</span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;iostream&gt;</span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> std;</span><br><span class="line"></span><br><span class="line"><span class="type">const</span> <span class="type">double</span> PI = <span class="number">3.14</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">class</span> <span class="title class_">Circle</span>&#123;</span><br><span class="line"><span class="comment">//访问权限</span></span><br><span class="line"><span class="keyword">private</span>:</span><br><span class="line">    <span class="comment">//属性</span></span><br><span class="line">    <span class="type">int</span> r;</span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">    <span class="function"><span class="type">void</span> <span class="title">setR</span><span class="params">(<span class="type">int</span> a)</span></span>&#123;</span><br><span class="line">        r = a;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">//行为</span></span><br><span class="line">    <span class="function"><span class="type">double</span> <span class="title">perimeter</span><span class="params">()</span></span>&#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="number">2</span>*PI*r;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="type">int</span> <span class="title">main</span><span class="params">()</span></span>&#123;</span><br><span class="line">    <span class="comment">//通过圆类创建具体的圆</span></span><br><span class="line">    Circle c1;</span><br><span class="line">    <span class="comment">//给圆对象的属性进行赋值</span></span><br><span class="line">    c1.<span class="built_in">setR</span>(<span class="number">5</span>);</span><br><span class="line">    cout &lt;&lt; <span class="string">&quot;The perimeter of c1 is: &quot;</span> &lt;&lt; c1.<span class="built_in">perimeter</span>() &lt;&lt; endl;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></li>
<li><p>将属性和行为加以不同的权限进行控制</p>
<blockquote>
<p>权限：1.<code>public</code>公共权限，类外可访问;2.<code>protected</code>保护权限，类外不可访问，可继承;3.<code>private</code>私有权限，类外不可访问，不可继承;</p>
</blockquote>
</li>
<li><p>class与struct：区别在于默认的访问权限不同；struct默认共有，class默认私有</p>
</li>
<li><p>成员属性设为私有</p>
<ul>
<li>可以自己控制读写权限</li>
<li>对于写权限，可以检测数据的有效性</li>
</ul>
</li>
</ul>
<h3 id="继承"><a href="#继承" class="headerlink" title="继承"></a>继承</h3><p>将成员间的共有特性抽离出来，利用继承的技术减少重复代码。</p>
<ul>
<li><p>语法：<code>·class 子类 : 继承方式 父类&#123;&#125;;</code>// 子类/派生类，父类/基类</p>
</li>
<li><p>继承方式：<code>public、protected、private</code></p>
</li>
<li><p>继承中构造和析构顺序：构造先父后子，析构相反。（白发人送黑发人QAQ）</p>
</li>
<li><p>继承同名成员处理：</p>
<ul>
<li>访问子类同名成员，直接访问即可</li>
<li>访问父类同名成员，需加载作用域<ul>
<li>访问父类同名成员，<code>object.Base::var;</code></li>
<li>访问父类同名函数，<code>object.Base::func();</code>。注：只要同名都需要加载作用域，不支持重载访问（<strong>子类会隐藏所有同名父类成员函数</strong>）</li>
</ul>
</li>
</ul>
</li>
<li><p>同名静态成员：访问<code>object</code>类下的继承父类的同名变量<code>var</code></p>
<ul>
<li><code>object::Base::var;</code></li>
</ul>
</li>
<li><p>多继承语法：<code>class 子类 : 继承方式 父类1, 父类2...&#123;&#125;;</code></p>
<ul>
<li>多继承中父类的同名函数需加作用域进行区分。<code>类名::同名函数</code></li>
<li>c++中尽量避免多继承的发生</li>
</ul>
</li>
<li><p>菱形继承：多继承时，两个以上的父类共有继承了同一个父类，导致内存重复开辟。</p>
<ul>
<li>虚继承可以避免重复继承的内存重复开销，语法：<code>class father1 : virtual 继承方式 grandpa&#123;&#125;;// class father2: virtual 继承方式 grandpa&#123;&#125;;// class son : 继承方式 father1,father2&#123;&#125;;</code></li>
<li><code>grandpa类</code>，又叫虚基类</li>
</ul>
</li>
</ul>
<h3 id="多态"><a href="#多态" class="headerlink" title="多态"></a>多态</h3><ul>
<li>静态多态：复用函数名，包括函数重载和运算符重载</li>
<li>动态多态：派生类和虚函数实现运行时多态</li>
<li>静态多态和动态多态的区别：<ul>
<li>静态多态的函数地址早绑定——编译阶段确定函数地址</li>
<li>动态多态的函数地址晚绑定——运行阶段确定函数地址</li>
</ul>
</li>
<li>纯虚函数和抽象类<ul>
<li>纯虚函数语法：<code>virtual 返回值类型 函数名(参数) = 0;</code></li>
<li>抽象类：类中存在纯虚函数。抽象类无法实例化，子类必须重写抽象类中的虚函数，否则子类也属于抽象类，也无法实例化。</li>
</ul>
</li>
<li>纯虚析构和虚析构<ul>
<li>虚析构语法：<code>virtual ~类名()&#123;&#125;</code></li>
<li>纯虚析构：<code>virtual ~类名()&#123;&#125;=0; // 类名::~类名()&#123;&#125;</code></li>
</ul>
</li>
</ul>
<h3 id="构造函数和析构函数"><a href="#构造函数和析构函数" class="headerlink" title="构造函数和析构函数"></a>构造函数和析构函数</h3><ul>
<li><p>由编译器自动调用完成初始化和清理工作。在默认情况下，C++编译器会自动添加3个函数：1.默认构造函数；2.默认析构函数；3.默认拷贝构造函数</p>
</li>
<li><p>如果用户自定义了拷贝构造函数，编译器不会再添加默认构造函数</p>
</li>
<li><p>构造函数：在对象创建时，为对象的成员属性赋值</p>
<blockquote>
<p>语法：<code>类名()&#123;&#125;</code></p>
<ul>
<li>没有返回值，也不写void</li>
<li>函数名称和类名相同</li>
<li>可以有参数，可以发生重载</li>
<li>程序调用对象时自动调用，且只会调用一次</li>
</ul>
</blockquote>
</li>
<li><p>析构函数：在对象销毁前，执行一些清理工作</p>
<blockquote>
<p>语法<code>~类名()&#123;&#125;</code></p>
<ul>
<li>没有返回值，也不写void</li>
<li>函数名称和类名相同，名称前加~</li>
<li>没有参数，不可以发生重载</li>
<li>程序在对象销毁时自动调用，且只会调用一次</li>
</ul>
</blockquote>
</li>
</ul>
<h3 id="深拷贝和浅拷贝"><a href="#深拷贝和浅拷贝" class="headerlink" title="深拷贝和浅拷贝"></a>深拷贝和浅拷贝</h3><ul>
<li>浅拷贝：简单的赋值拷贝操作；在堆区内存释放时，可能会造成内存重复释放</li>
<li>深拷贝：在堆区重新申请空间，进行拷贝；在堆区重新开辟值相同的内存</li>
</ul>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;iostream&gt;</span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> std;</span><br><span class="line"></span><br><span class="line"><span class="keyword">class</span> <span class="title class_">Person</span>&#123;</span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">    <span class="comment">// 构造函数</span></span><br><span class="line">    <span class="built_in">Person</span>(<span class="type">int</span> a, <span class="type">int</span> h)&#123;</span><br><span class="line">        age = a;</span><br><span class="line">        height = <span class="keyword">new</span> <span class="built_in">int</span>(h);</span><br><span class="line">        cout &lt;&lt; <span class="string">&quot;构造函数调用&quot;</span> &lt;&lt; endl;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">// 拷贝构造函数</span></span><br><span class="line">    <span class="built_in">Person</span>(<span class="type">const</span> Person &amp;p)&#123;</span><br><span class="line">        age = p.age; <span class="comment">// 浅拷贝，不重新开辟内存</span></span><br><span class="line">        height = <span class="keyword">new</span> <span class="built_in">int</span>(*p.height);<span class="comment">// 深拷贝，重新开辟内存</span></span><br><span class="line">        cout &lt;&lt; <span class="string">&quot;拷贝构造函数调用&quot;</span> &lt;&lt; endl;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">// 析构函数，一般用于释放堆区开辟的内存</span></span><br><span class="line">    ~<span class="built_in">Person</span>()&#123;</span><br><span class="line">       <span class="keyword">if</span>(height != <span class="literal">NULL</span>)&#123;</span><br><span class="line">           <span class="keyword">delete</span> height;</span><br><span class="line">           height = <span class="literal">NULL</span>; <span class="comment">// 避免出现野指针</span></span><br><span class="line">       &#125; </span><br><span class="line">        cout &lt;&lt; <span class="string">&quot;析构函数调用.&quot;</span> &lt;&lt; endl;</span><br><span class="line">    &#125;</span><br><span class="line"><span class="keyword">private</span>:</span><br><span class="line">    <span class="type">int</span> age;</span><br><span class="line">    <span class="type">int</span> *height;</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="type">void</span> <span class="title">test</span><span class="params">()</span></span>&#123;</span><br><span class="line">    <span class="function">Person <span class="title">p1</span><span class="params">(<span class="number">18</span>,<span class="number">180</span>)</span></span>;</span><br><span class="line">    cout &lt;&lt; <span class="string">&quot;--------&quot;</span> &lt;&lt; endl;</span><br><span class="line">    <span class="function">Person <span class="title">p2</span><span class="params">(p1)</span></span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="type">int</span> <span class="title">main</span><span class="params">()</span></span>&#123;</span><br><span class="line">    <span class="built_in">test</span>();</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="类对象作为类成员"><a href="#类对象作为类成员" class="headerlink" title="类对象作为类成员"></a>类对象作为类成员</h3><p>当其他类对象作为本类成员，构造时先构造类对象，析构顺序相反</p>
<h3 id="静态成员"><a href="#静态成员" class="headerlink" title="静态成员"></a>静态成员</h3><p>在成员变量或成员函数前加上关键字static，静态成员不属于具体对象上，被所有对象共享。</p>
<ul>
<li>静态成员变量<ul>
<li>所有对象共享同一份数据</li>
<li>在编译阶段分配内存</li>
<li><strong>类内声明，类外初始化</strong></li>
</ul>
</li>
<li>静态成员函数<ul>
<li>所有对象共享同一个函数</li>
<li>静态成员函数只能访问静态成员变量</li>
</ul>
</li>
</ul>
<h3 id="this指针"><a href="#this指针" class="headerlink" title="this指针"></a>this指针</h3><p>c++中成员变量和成员函数时分开存储的，每一个静态成员函数只会诞生一份函数实例，也就是说多个类型的对象会公用一块代码。c++中通过this指针指向被调用成员函数所属对象来实现区分。</p>
<ul>
<li>当形参和成员变量同名时，可用this指针来区分</li>
<li>在类的静态成员函数中返回对象本身，可用<code>return *this</code></li>
</ul>
<h3 id="常函数"><a href="#常函数" class="headerlink" title="常函数"></a>常函数</h3><p>成员函数后加<code>const</code></p>
<ul>
<li>常函数内不可修改成员属性</li>
<li>成员属性声明时加关键字<code>mutable</code>后，在常函数中可以修改</li>
</ul>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">class</span> <span class="title class_">Person</span>&#123;</span><br><span class="line">    <span class="function"><span class="type">void</span> <span class="title">property</span><span class="params">()</span> <span class="type">const</span></span>&#123;</span><br><span class="line">      <span class="comment">// this-&gt;b = 100; // 不能修改</span></span><br><span class="line">        <span class="keyword">this</span>-&gt;a = <span class="number">100</span>;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">mutable</span> <span class="type">int</span> a;</span><br><span class="line">    <span class="type">int</span> b;</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>

<h3 id="常对象"><a href="#常对象" class="headerlink" title="常对象"></a>常对象</h3><p>声明对象前加<code>const</code></p>
<ul>
<li>常对象只能调用常函数</li>
</ul>
<h3 id="空指针访问成员函数"><a href="#空指针访问成员函数" class="headerlink" title="空指针访问成员函数"></a>空指针访问成员函数</h3><p>c++中允许空指针调用成员函数，但注意空指针调用时不可使用成员对象，否则会保存。</p>
<ul>
<li>空指针没有用到this指针，为了代码鲁棒性可添加<code>if(this==NULL)&#123;return;&#125;</code>，避免空指针调用出错。</li>
</ul>
<h3 id="友元"><a href="#友元" class="headerlink" title="友元"></a>友元</h3><p>让一个函数或类访问另一个类中私有成员，关键字为<code>friend</code></p>
<ul>
<li>全局友元</li>
<li>类做友元</li>
<li>成员函数做友元</li>
</ul>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">class</span> <span class="title class_">Building</span>&#123;</span><br><span class="line">    <span class="comment">// 全局函数 友元声明</span></span><br><span class="line">    <span class="function"><span class="keyword">friend</span> <span class="type">void</span> <span class="title">goodMan</span><span class="params">(Building *building)</span></span>;</span><br><span class="line">    <span class="comment">// 类 友元声明</span></span><br><span class="line">    <span class="keyword">friend</span> <span class="keyword">class</span> <span class="title class_">goodMan</span>;</span><br><span class="line">    <span class="comment">// 成员函数 友元声明</span></span><br><span class="line">    <span class="function"><span class="keyword">friend</span> <span class="type">void</span> <span class="title">goodMan::visit</span><span class="params">()</span></span>; </span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">    <span class="comment">// 构造函数</span></span><br><span class="line">    <span class="built_in">Building</span>()&#123;</span><br><span class="line">        sittingRoom = <span class="string">&quot;客厅&quot;</span>;</span><br><span class="line">        string bedRoom = <span class="string">&quot;卧室&quot;</span>;</span><br><span class="line">    &#125;</span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">    string sittingRoom;</span><br><span class="line"><span class="keyword">private</span>:</span><br><span class="line">    string bedRoom;</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="keyword">class</span> <span class="title class_">goodMan</span>&#123;</span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">    Building *building;</span><br><span class="line">    <span class="function"><span class="type">void</span> <span class="title">visit</span><span class="params">()</span></span>;</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="comment">// 类外写成员函数</span></span><br><span class="line"><span class="function"><span class="type">void</span> <span class="title">goodMan::visit</span><span class="params">()</span></span>&#123;</span><br><span class="line">    cout &lt;&lt; building-&gt;sittingRom &lt;&lt; endl;</span><br><span class="line">    cout &lt;&lt; building-&gt;bedRoom &lt;&lt; endl;</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">// 类外构造函数</span></span><br><span class="line">goodMan::<span class="built_in">goodMan</span>()&#123;</span><br><span class="line">    buiding =  <span class="keyword">new</span> Building;</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">// 类外析构函数</span></span><br><span class="line">goodMan::~<span class="built_in">goodMan</span>()&#123;</span><br><span class="line">    <span class="keyword">if</span>(building != <span class="literal">NULL</span>)&#123;</span><br><span class="line">        <span class="keyword">delete</span> building;</span><br><span class="line">        building = <span class="literal">NULL</span>；</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">// 全局函数</span></span><br><span class="line"><span class="type">void</span> <span class="built_in">goodMan</span>(Building *building)&#123;</span><br><span class="line">    cout &lt;&lt; building-&gt;sittingRom &lt;&lt; endl;</span><br><span class="line">    cout &lt;&lt; building-&gt;bedRoom &lt;&lt; endl;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="运算符重载"><a href="#运算符重载" class="headerlink" title="运算符重载"></a>运算符重载</h3><p>对已有的运算符重新进行定义，赋予其另一种功能，以适应不同的数据类型</p>
<ul>
<li><p>可重载运算符</p>
<table>
<thead>
<tr>
<th align="center">运算符</th>
<th align="center">符号</th>
</tr>
</thead>
<tbody><tr>
<td align="center">双目算术运算符</td>
<td align="center">+ (加)，-(减)，*(乘)，/(除)，% (取模)</td>
</tr>
<tr>
<td align="center">关系运算符</td>
<td align="center">==(等于)，!= (不等于)，&lt; (小于)，&gt; (大于)，&lt;=(小于等于)，&gt;=(大于等于)</td>
</tr>
<tr>
<td align="center">逻辑运算符</td>
<td align="center">||(逻辑或)，&amp;&amp;(逻辑与)，!(逻辑非)</td>
</tr>
<tr>
<td align="center">单目运算符</td>
<td align="center">+ (正)，-(负)，*(指针)，&amp;(取地址)</td>
</tr>
<tr>
<td align="center">自增自减运算符</td>
<td align="center">++(自增)，–(自减)</td>
</tr>
<tr>
<td align="center">位运算符</td>
<td align="center">| (按位或)，&amp; (按位与)，~(按位取反)，^(按位异或),，&lt;&lt; (左移)，&gt;&gt;(右移)</td>
</tr>
<tr>
<td align="center">赋值运算符</td>
<td align="center">=, +=, -=, *=, /= , % = , &amp;=, |=, ^=, &lt;&lt;=, &gt;&gt;=</td>
</tr>
<tr>
<td align="center">空间申请与释放</td>
<td align="center">new, delete, new[ ] , delete[]</td>
</tr>
<tr>
<td align="center">其他运算符</td>
<td align="center"><strong>()</strong>(函数调用)，**-&gt;<strong>(成员访问)，</strong>,<strong>(逗号)，</strong>[]**(下标)</td>
</tr>
</tbody></table>
</li>
<li><p>不可重载运算符</p>
<table>
<thead>
<tr>
<th align="center">名称</th>
<th align="center">符号</th>
</tr>
</thead>
<tbody><tr>
<td align="center">成员访问运算符</td>
<td align="center"><strong>.</strong></td>
</tr>
<tr>
<td align="center">成员指针访问运算符</td>
<td align="center"><strong>.*</strong></td>
</tr>
<tr>
<td align="center">域运算符</td>
<td align="center"><strong>::</strong></td>
</tr>
<tr>
<td align="center">条件运算符</td>
<td align="center"><strong>?:</strong></td>
</tr>
<tr>
<td align="center">预处理符号</td>
<td align="center"><strong>#</strong></td>
</tr>
</tbody></table>
</li>
<li><p>语法：<code>类型 operator预算符(参数)&#123;&#125;</code></p>
<ul>
<li>重载可分<strong>类内</strong>重载和<strong>类外</strong>重载，调用方式可能有所区别</li>
</ul>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">### 重载左移运算符&lt;&lt;</span><br><span class="line">ostream &amp; ostream::<span class="keyword">operator</span> &lt;&lt; (<span class="type">const</span> <span class="type">char</span>* s)</span><br><span class="line">&#123;</span><br><span class="line">    <span class="comment">//输出s的代码</span></span><br><span class="line">    <span class="keyword">return</span> * <span class="keyword">this</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<ul>
<li><code>()</code>重载/仿函数</li>
</ul>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;iostream&gt;</span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> std;</span><br><span class="line"> </span><br><span class="line"><span class="keyword">class</span> <span class="title class_">Distance</span></span><br><span class="line">&#123;</span><br><span class="line">   <span class="keyword">private</span>:</span><br><span class="line">      <span class="type">int</span> feet;             <span class="comment">// 0 到无穷</span></span><br><span class="line">      <span class="type">int</span> inches;           <span class="comment">// 0 到 12</span></span><br><span class="line">   <span class="keyword">public</span>:</span><br><span class="line">      <span class="comment">// 所需的构造函数</span></span><br><span class="line">      <span class="built_in">Distance</span>()&#123;</span><br><span class="line">         feet = <span class="number">0</span>;</span><br><span class="line">         inches = <span class="number">0</span>;</span><br><span class="line">      &#125;</span><br><span class="line">      <span class="built_in">Distance</span>(<span class="type">int</span> f, <span class="type">int</span> i)&#123;</span><br><span class="line">         feet = f;</span><br><span class="line">         inches = i;</span><br><span class="line">      &#125;</span><br><span class="line">      <span class="comment">// 重载函数调用运算符</span></span><br><span class="line">      <span class="function">Distance <span class="title">operator</span><span class="params">()</span><span class="params">(<span class="type">int</span> a, <span class="type">int</span> b, <span class="type">int</span> c)</span></span></span><br><span class="line"><span class="function">      </span>&#123;</span><br><span class="line">         Distance D;</span><br><span class="line">         <span class="comment">// 进行随机计算</span></span><br><span class="line">         D.feet = a + c + <span class="number">10</span>;</span><br><span class="line">         D.inches = b + c + <span class="number">100</span> ;</span><br><span class="line">         <span class="keyword">return</span> D;</span><br><span class="line">      &#125;</span><br><span class="line">      <span class="comment">// 显示距离的方法</span></span><br><span class="line">      <span class="function"><span class="type">void</span> <span class="title">displayDistance</span><span class="params">()</span></span></span><br><span class="line"><span class="function">      </span>&#123;</span><br><span class="line">         cout &lt;&lt; <span class="string">&quot;F: &quot;</span> &lt;&lt; feet &lt;&lt;  <span class="string">&quot; I:&quot;</span> &lt;&lt;  inches &lt;&lt; endl;</span><br><span class="line">      &#125;</span><br><span class="line">      </span><br><span class="line">&#125;;</span><br><span class="line"><span class="function"><span class="type">int</span> <span class="title">main</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">   <span class="function">Distance <span class="title">D1</span><span class="params">(<span class="number">11</span>, <span class="number">10</span>)</span>, D2</span>;</span><br><span class="line"></span><br><span class="line">   cout &lt;&lt; <span class="string">&quot;First Distance : &quot;</span>; </span><br><span class="line">   D1.<span class="built_in">displayDistance</span>();</span><br><span class="line"></span><br><span class="line">   D2 = <span class="built_in">D1</span>(<span class="number">10</span>, <span class="number">10</span>, <span class="number">10</span>); <span class="comment">// invoke operator()</span></span><br><span class="line">   cout &lt;&lt; <span class="string">&quot;Second Distance :&quot;</span>; </span><br><span class="line">   D2.<span class="built_in">displayDistance</span>();</span><br><span class="line"></span><br><span class="line">   <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></li>
</ul>
<h3 id="文件操作"><a href="#文件操作" class="headerlink" title="文件操作"></a>文件操作</h3><ul>
<li> 文件读取流和向文件写入流 <code>fstream</code></li>
</ul>
<table>
<thead>
<tr>
<th align="center">据类型</th>
<th align="center">描述</th>
</tr>
</thead>
<tbody><tr>
<td align="center">ofstream</td>
<td align="center">该数据类型表示输出文件流，用于创建文件并向文件写入信息。</td>
</tr>
<tr>
<td align="center">ifstream</td>
<td align="center">该数据类型表示输入文件流，用于从文件读取信息。</td>
</tr>
<tr>
<td align="center">fstream</td>
<td align="center">该数据类型通常表示文件流，且同时具有 ofstream 和 ifstream 两种功能，这意味着它可以创建文件，向文件写入信息，从文件读取信息。</td>
</tr>
</tbody></table>
<ul>
<li>打开方式。多模式混合使用，如二进制打开文件<code>iso::out | iso::binary</code></li>
</ul>
<table>
<thead>
<tr>
<th align="left">模式标志</th>
<th align="left">描述</th>
</tr>
</thead>
<tbody><tr>
<td align="left">ios::app</td>
<td align="left">追加模式。所有写入都追加到文件末尾。</td>
</tr>
<tr>
<td align="left">ios::ate</td>
<td align="left">文件打开后定位到文件末尾。</td>
</tr>
<tr>
<td align="left">ios::in</td>
<td align="left">打开文件用于读取。</td>
</tr>
<tr>
<td align="left">ios::out</td>
<td align="left">打开文件用于写入。</td>
</tr>
<tr>
<td align="left">ios::trunc</td>
<td align="left">如果该文件已经存在，其内容将在打开文件之前被截断，即把文件长度设为 0。</td>
</tr>
<tr>
<td align="left">ios::binary</td>
<td align="left">以二进制方式打开</td>
</tr>
</tbody></table>
<ul>
<li>打开文件<ul>
<li> 在从文件读取信息或者向文件写入信息之前，必须先打开文件。<strong>ofstream</strong> 和 <strong>fstream</strong> 对象都可以用来打开文件进行写操作，如果只需要打开文件进行读操作，则使用 <strong>ifstream</strong> 对象。 </li>
<li> <code>void open(const char *filename, ios::openmode mode);</code></li>
<li>以写入模式打开文件，并希望截断文件，以防文件已存在 <code>ofstream outfile;  outfile.open(&quot;file.dat&quot;, ios::out | ios::trunc );</code></li>
<li> 打开一个文件用于读写 <code>ifstream  afile;afile.open(&quot;file.dat&quot;, ios::out | ios::in );</code></li>
</ul>
</li>
<li>关闭文件<ul>
<li> 当 C++ 程序终止时，它会自动关闭刷新所有流，释放所有分配的内存，并关闭所有打开的文件。但程序员应该养成一个好习惯，在程序终止前关闭所有打开的文件。 <code>viod close();</code></li>
</ul>
</li>
<li>写入文件<ul>
<li> 在 C++ 编程中，我们使用流插入运算符（ &lt;&lt; ）向文件写入信息，就像使用该运算符输出信息到屏幕上一样。唯一不同的是，在这里您使用的是 <strong>ofstream</strong> 或 <strong>fstream</strong> 对象，而不是 <strong>cout</strong> 对象。 </li>
</ul>
</li>
<li>读取文件<ul>
<li> 在 C++ 编程中，我们使用流提取运算符（ &gt;&gt; ）从文件读取信息，就像使用该运算符从键盘输入信息一样。唯一不同的是，在这里您使用的是 <strong>ifstream</strong> 或 <strong>fstream</strong> 对象，而不是 <strong>cin</strong> 对象。 </li>
</ul>
</li>
<li>例</li>
</ul>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;fstream&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;iostream&gt;</span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> std;</span><br><span class="line"> </span><br><span class="line"><span class="function"><span class="type">int</span> <span class="title">main</span> <span class="params">()</span></span>&#123;</span><br><span class="line">   <span class="type">char</span> data[<span class="number">100</span>];</span><br><span class="line">   <span class="comment">// 以写模式打开文件</span></span><br><span class="line">   ofstream outfile;</span><br><span class="line">   outfile.<span class="built_in">open</span>(<span class="string">&quot;afile.dat&quot;</span>);</span><br><span class="line"> </span><br><span class="line">   cout &lt;&lt; <span class="string">&quot;Writing to the file&quot;</span> &lt;&lt; endl;</span><br><span class="line">   cout &lt;&lt; <span class="string">&quot;Enter your name: &quot;</span>; </span><br><span class="line">   cin.<span class="built_in">getline</span>(data, <span class="number">100</span>);</span><br><span class="line"> </span><br><span class="line">   <span class="comment">// 向文件写入用户输入的数据</span></span><br><span class="line">   outfile &lt;&lt; data &lt;&lt; endl;</span><br><span class="line"> </span><br><span class="line">   cout &lt;&lt; <span class="string">&quot;Enter your age: &quot;</span>; </span><br><span class="line">   cin &gt;&gt; data;</span><br><span class="line">   cin.<span class="built_in">ignore</span>();</span><br><span class="line">   </span><br><span class="line">   <span class="comment">// 再次向文件写入用户输入的数据</span></span><br><span class="line">   outfile &lt;&lt; data &lt;&lt; endl;</span><br><span class="line"> </span><br><span class="line">   <span class="comment">// 关闭打开的文件</span></span><br><span class="line">   outfile.<span class="built_in">close</span>();</span><br><span class="line"> </span><br><span class="line">   <span class="comment">// 以读模式打开文件</span></span><br><span class="line">   ifstream infile; </span><br><span class="line">   infile.<span class="built_in">open</span>(<span class="string">&quot;afile.dat&quot;</span>); </span><br><span class="line"> </span><br><span class="line">   cout &lt;&lt; <span class="string">&quot;Reading from the file&quot;</span> &lt;&lt; endl; </span><br><span class="line">   infile &gt;&gt; data; </span><br><span class="line"> </span><br><span class="line">   <span class="comment">// 在屏幕上写入数据</span></span><br><span class="line">   cout &lt;&lt; data &lt;&lt; endl;</span><br><span class="line">   </span><br><span class="line">   <span class="comment">// 再次从文件读取数据，并显示它</span></span><br><span class="line">   infile &gt;&gt; data; </span><br><span class="line">   cout &lt;&lt; data &lt;&lt; endl; </span><br><span class="line"> </span><br><span class="line">   <span class="comment">// 关闭打开的文件</span></span><br><span class="line">   infile.<span class="built_in">close</span>();</span><br><span class="line"> </span><br><span class="line">   <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>



<h3 id="模板"><a href="#模板" class="headerlink" title="模板"></a>模板</h3><h3 id="容器操作"><a href="#容器操作" class="headerlink" title="容器操作"></a>容器操作</h3><ul>
<li>vector 容器</li>
</ul>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;iostream&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;vector&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;algorithm&gt;</span></span></span><br><span class="line"></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> std;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="type">void</span> <span class="title">main</span><span class="params">()</span></span>&#123;</span><br><span class="line">    <span class="comment">// 创建vector容器对象</span></span><br><span class="line">    vector&lt;<span class="type">int</span>&gt; v;</span><br><span class="line">    v.<span class="built_in">push_back</span>(<span class="number">10</span>);</span><br><span class="line">    v.<span class="built_in">push_back</span>(<span class="number">20</span>);</span><br><span class="line">    v.<span class="built_in">push_back</span>(<span class="number">30</span>);</span><br><span class="line">    v.<span class="built_in">push_back</span>(<span class="number">40</span>);</span><br><span class="line">    </span><br><span class="line">    <span class="comment">//每个容器都有自己的迭代器，迭代器用来遍历容器中的元素</span></span><br><span class="line">    <span class="comment">//v.begin()返回迭代器，指向容器中第一个数据；v.end()返回迭代器，指向容器中最后一个数据的下一个位置</span></span><br><span class="line">    <span class="comment">// vector&lt;int&gt;::iterator 拿到迭代器类型</span></span><br><span class="line">    </span><br><span class="line">     <span class="comment">//遍历</span></span><br><span class="line">    <span class="comment">// ---------------------1</span></span><br><span class="line">    vector&lt;<span class="type">int</span>&gt;::iterator pbegin = v,<span class="built_in">begin</span>();</span><br><span class="line">    vector&lt;<span class="type">int</span>&gt;::iterator pend = v.<span class="built_in">end</span>();</span><br><span class="line">    <span class="keyword">while</span>(pbegin != pend)&#123;</span><br><span class="line">        cout &lt;&lt; *pbegin &lt;&lt; enl;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">//-----------------------2</span></span><br><span class="line">    <span class="keyword">for</span>(vector::iterator it = v.<span class="built_in">begin</span>();it != v.<span class="built_in">end</span>();it++)&#123;</span><br><span class="line">        cout &lt;&lt; *it &lt;&lt; endl;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">//-----------------------3</span></span><br><span class="line">    for_each(v.<span class="built_in">begin</span>(),v.<span class="built_in">end</span>(),[](<span class="type">int</span> val)&#123;cout &lt;&lt; val &lt;&lt;endl;&#125;)</span><br><span class="line"> &#125;</span><br></pre></td></tr></table></figure>



<h3 id="C-11"><a href="#C-11" class="headerlink" title="C++11"></a>C++11</h3><ul>
<li><code>auto</code> 从初始化表达式中推断变量的类型，不影响运行效率</li>
</ul>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">auto</span> i = <span class="number">10</span>; <span class="comment">// int</span></span><br><span class="line"><span class="keyword">auto</span> str  = <span class="string">&quot;c++&quot;</span>; <span class="comment">// const char*</span></span><br><span class="line"><span class="keyword">auto</span> p = <span class="keyword">new</span> <span class="built_in">Person</span>(); <span class="comment">// person *</span></span><br></pre></td></tr></table></figure>

<ul>
<li><code>decltype</code> 可以获取变量的类型</li>
</ul>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="type">int</span> a = <span class="number">10</span>;</span><br><span class="line"><span class="keyword">decltype</span>(a) b = <span class="number">20</span>; <span class="comment">// int</span></span><br></pre></td></tr></table></figure>

<ul>
<li><code>nullptr</code>空指针。解决<code>NULL</code>赋值可能存在二义性的问题</li>
<li>快速遍历 // 间接的初始化方式</li>
</ul>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="type">int</span> array[]&#123;<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>,<span class="number">4</span>,<span class="number">5</span>&#125;; <span class="comment">//int array[] = &#123;1,2,3,4,5&#125;;</span></span><br><span class="line"><span class="keyword">for</span>(<span class="type">int</span> item:array)&#123;</span><br><span class="line">    cout &lt;&lt; item </span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<ul>
<li><code>lambda</code>表达式：<code>auto func = [capture list](params list) mutable exception -&gt; return type&#123;function body&#125;</code><ul>
<li>capture list: 捕获外部变量列表；<code>[=]</code>捕获用到的外部变量，值捕获;<code>[a,b]</code>捕获外部变量<code>a,b</code> /值捕获;<code>[&amp;]</code>地址捕获；<code>[&amp;,a]</code>a是值捕获，其他变量地址捕获</li>
<li>params list: 参数列表，可省略</li>
<li>mutable：说明是否可以修改捕获的变量，可省略；类似将捕获的值，生成内部的局部变量</li>
<li>exception：异常设定，可省略</li>
<li>return type： 返回值类型，可省略</li>
</ul>
</li>
</ul>
<h3 id="参考链接"><a href="#参考链接" class="headerlink" title="参考链接"></a>参考链接</h3><font size=2>

<p>[1] <a target="_blank" rel="noopener" href="https://www.bilibili.com/video/BV1et411b73Z?p=8&spm_id_from=pageDriver">黑马程序员 C++</a></p>
<p>[2] <a target="_blank" rel="noopener" href="https://www.runoob.com/cplusplus/cpp-data-types.html">C++ 菜鸟</a></p>
<p>[] <a href=""></a></p>
</font>


    </div>

    
    
    

      <footer class="post-footer">
          
          <div class="post-tags">
              <a href="/blog/tags/C/" rel="tag"><i class="fa fa-tag"></i> C++</a>
          </div>

        


        
    <div class="post-nav">
      <div class="post-nav-item">
    <a href="/blog/2021/09/01/%E9%A1%B9%E7%9B%AE%E5%88%97%E8%A1%A8/" rel="prev" title="项目列表">
      <i class="fa fa-chevron-left"></i> 项目列表
    </a></div>
      <div class="post-nav-item">
    <a href="/blog/2021/09/05/shell/" rel="next" title="shell">
      shell <i class="fa fa-chevron-right"></i>
    </a></div>
    </div>
      </footer>
    
  </article>
  
  
  



          </div>
          
    <div class="comments" id="valine-comments"></div>

<script>
  window.addEventListener('tabs:register', () => {
    let { activeClass } = CONFIG.comments;
    if (CONFIG.comments.storage) {
      activeClass = localStorage.getItem('comments_active') || activeClass;
    }
    if (activeClass) {
      let activeTab = document.querySelector(`a[href="#comment-${activeClass}"]`);
      if (activeTab) {
        activeTab.click();
      }
    }
  });
  if (CONFIG.comments.storage) {
    window.addEventListener('tabs:click', event => {
      if (!event.target.matches('.tabs-comment .tab-content .tab-pane')) return;
      let commentClass = event.target.classList[1];
      localStorage.setItem('comments_active', commentClass);
    });
  }
</script>


        </div>
          
  
  <div class="toggle sidebar-toggle">
    <span class="toggle-line toggle-line-first"></span>
    <span class="toggle-line toggle-line-middle"></span>
    <span class="toggle-line toggle-line-last"></span>
  </div>

  <aside class="sidebar">
    <div class="sidebar-inner">

      <ul class="sidebar-nav motion-element">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <!--noindex-->
      <div class="post-toc-wrap sidebar-panel">
          <div class="post-toc motion-element"><ol class="nav"><li class="nav-item nav-level-3"><a class="nav-link" href="#HelloWorld"><span class="nav-number">1.</span> <span class="nav-text">HelloWorld</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E6%B3%A8%E9%87%8A"><span class="nav-number">2.</span> <span class="nav-text">注释</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E5%B8%B8%E9%87%8F"><span class="nav-number">3.</span> <span class="nav-text">常量</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E5%85%B3%E9%94%AE%E5%AD%97%EF%BC%88%E4%BF%9D%E7%95%99%E5%AD%97%EF%BC%89"><span class="nav-number">4.</span> <span class="nav-text">关键字（保留字）</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E6%A0%87%E8%AF%86%E7%AC%A6%E5%91%BD%E5%90%8D%E8%A7%84%E5%88%99"><span class="nav-number">5.</span> <span class="nav-text">标识符命名规则</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E6%95%B0%E6%8D%AE%E7%B1%BB%E5%9E%8B"><span class="nav-number">6.</span> <span class="nav-text">数据类型</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E5%9F%BA%E6%9C%AC%E6%95%B0%E6%8D%AE%E7%B1%BB%E5%9E%8B"><span class="nav-number">7.</span> <span class="nav-text">基本数据类型</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E5%90%84%E6%95%B0%E6%8D%AE%E7%B1%BB%E5%9E%8B%E5%8D%A0%E7%94%A8%E7%A9%BA%E9%97%B4"><span class="nav-number">8.</span> <span class="nav-text">各数据类型占用空间</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#sizeof%E3%80%81typedf%E3%80%81enum"><span class="nav-number">9.</span> <span class="nav-text">sizeof、typedf、enum</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E5%AD%97%E7%AC%A6%E4%B8%8E%E5%AD%97%E7%AC%A6%E4%B8%B2"><span class="nav-number">10.</span> <span class="nav-text">字符与字符串</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E8%BD%AC%E4%B9%89%E5%AD%97%E7%AC%A6"><span class="nav-number">11.</span> <span class="nav-text">转义字符</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E6%95%B0%E6%8D%AE%E7%9A%84%E8%BE%93%E5%85%A5cin"><span class="nav-number">12.</span> <span class="nav-text">数据的输入cin</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E4%B8%89%E7%9B%AE%E8%BF%90%E7%AE%97%E7%AC%A6"><span class="nav-number">13.</span> <span class="nav-text">三目运算符</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E7%B1%BB%E5%9E%8B%E8%BD%AC%E6%8D%A2"><span class="nav-number">14.</span> <span class="nav-text">类型转换</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E5%87%BD%E6%95%B0%E7%9A%84%E5%88%86%E6%96%87%E4%BB%B6%E7%BC%96%E5%86%99"><span class="nav-number">15.</span> <span class="nav-text">函数的分文件编写</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E6%8C%87%E9%92%88"><span class="nav-number">16.</span> <span class="nav-text">指针</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E7%BB%93%E6%9E%84%E4%BD%93"><span class="nav-number">17.</span> <span class="nav-text">结构体</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E5%86%85%E5%AD%98%E5%88%86%E5%8C%BA%E6%A8%A1%E5%9E%8B"><span class="nav-number">18.</span> <span class="nav-text">内存分区模型</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E5%BC%95%E7%94%A8"><span class="nav-number">19.</span> <span class="nav-text">引用</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E5%B0%81%E8%A3%85"><span class="nav-number">20.</span> <span class="nav-text">封装</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E7%BB%A7%E6%89%BF"><span class="nav-number">21.</span> <span class="nav-text">继承</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E5%A4%9A%E6%80%81"><span class="nav-number">22.</span> <span class="nav-text">多态</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E6%9E%84%E9%80%A0%E5%87%BD%E6%95%B0%E5%92%8C%E6%9E%90%E6%9E%84%E5%87%BD%E6%95%B0"><span class="nav-number">23.</span> <span class="nav-text">构造函数和析构函数</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E6%B7%B1%E6%8B%B7%E8%B4%9D%E5%92%8C%E6%B5%85%E6%8B%B7%E8%B4%9D"><span class="nav-number">24.</span> <span class="nav-text">深拷贝和浅拷贝</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E7%B1%BB%E5%AF%B9%E8%B1%A1%E4%BD%9C%E4%B8%BA%E7%B1%BB%E6%88%90%E5%91%98"><span class="nav-number">25.</span> <span class="nav-text">类对象作为类成员</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E9%9D%99%E6%80%81%E6%88%90%E5%91%98"><span class="nav-number">26.</span> <span class="nav-text">静态成员</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#this%E6%8C%87%E9%92%88"><span class="nav-number">27.</span> <span class="nav-text">this指针</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E5%B8%B8%E5%87%BD%E6%95%B0"><span class="nav-number">28.</span> <span class="nav-text">常函数</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E5%B8%B8%E5%AF%B9%E8%B1%A1"><span class="nav-number">29.</span> <span class="nav-text">常对象</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E7%A9%BA%E6%8C%87%E9%92%88%E8%AE%BF%E9%97%AE%E6%88%90%E5%91%98%E5%87%BD%E6%95%B0"><span class="nav-number">30.</span> <span class="nav-text">空指针访问成员函数</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E5%8F%8B%E5%85%83"><span class="nav-number">31.</span> <span class="nav-text">友元</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E8%BF%90%E7%AE%97%E7%AC%A6%E9%87%8D%E8%BD%BD"><span class="nav-number">32.</span> <span class="nav-text">运算符重载</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E6%96%87%E4%BB%B6%E6%93%8D%E4%BD%9C"><span class="nav-number">33.</span> <span class="nav-text">文件操作</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E6%A8%A1%E6%9D%BF"><span class="nav-number">34.</span> <span class="nav-text">模板</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E5%AE%B9%E5%99%A8%E6%93%8D%E4%BD%9C"><span class="nav-number">35.</span> <span class="nav-text">容器操作</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#C-11"><span class="nav-number">36.</span> <span class="nav-text">C++11</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E5%8F%82%E8%80%83%E9%93%BE%E6%8E%A5"><span class="nav-number">37.</span> <span class="nav-text">参考链接</span></a></li></ol></div>
      </div>
      <!--/noindex-->

      <div class="site-overview-wrap sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
    <img class="site-author-image" itemprop="image" alt="bgape002"
      src="/blog/images/head.png">
  <p class="site-author-name" itemprop="name">bgape002</p>
  <div class="site-description" itemprop="description">mail: bgape002@163.com</div>
</div>
<div class="site-state-wrap motion-element">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
          <a href="/blog/archives/">
        
          <span class="site-state-item-count">61</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
            <a href="/blog/categories/">
          
        <span class="site-state-item-count">27</span>
        <span class="site-state-item-name">分类</span></a>
      </div>
      <div class="site-state-item site-state-tags">
            <a href="/blog/tags/">
          
        <span class="site-state-item-count">66</span>
        <span class="site-state-item-name">标签</span></a>
      </div>
  </nav>
</div>



      </div>

    </div>
  </aside>
  <div id="sidebar-dimmer"></div>


      </div>
    </main>

    <footer class="footer">
      <div class="footer-inner">
        

        

<div class="copyright">
  
  &copy; 2021 – 
  <span itemprop="copyrightYear">2023</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">bgape002</span>
</div>
  <div class="powered-by">由 <a href="https://hexo.io/" class="theme-link" rel="noopener" target="_blank">Hexo</a> & <a href="https://theme-next.org/" class="theme-link" rel="noopener" target="_blank">NexT.Gemini</a> 强力驱动
  </div>

        
<div class="busuanzi-count">
  <script async src="https://busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>
    <span class="post-meta-item" id="busuanzi_container_site_uv" style="display: none;">
      <span class="post-meta-item-icon">
        <i class="fa fa-user"></i>
      </span>
      <span class="site-uv" title="总访客量">
        <span id="busuanzi_value_site_uv"></span>
      </span>
    </span>
    <span class="post-meta-divider">|</span>
    <span class="post-meta-item" id="busuanzi_container_site_pv" style="display: none;">
      <span class="post-meta-item-icon">
        <i class="fa fa-eye"></i>
      </span>
      <span class="site-pv" title="总访问量">
        <span id="busuanzi_value_site_pv"></span>
      </span>
    </span>
</div>








      </div>
    </footer>
  </div>

  
  <script src="/blog/lib/anime.min.js"></script>
  <script src="/blog/lib/velocity/velocity.min.js"></script>
  <script src="/blog/lib/velocity/velocity.ui.min.js"></script>

<script src="/blog/js/utils.js"></script>

<script src="/blog/js/motion.js"></script>


<script src="/blog/js/schemes/pisces.js"></script>


<script src="/blog/js/next-boot.js"></script>




  




  
<script src="/blog/js/local-search.js"></script>











<script>
if (document.querySelectorAll('pre.mermaid').length) {
  NexT.utils.getScript('//cdn.jsdelivr.net/npm/mermaid@8/dist/mermaid.min.js', () => {
    mermaid.initialize({
      theme    : 'forest',
      logLevel : 3,
      flowchart: { curve     : 'linear' },
      gantt    : { axisFormat: '%m/%d/%Y' },
      sequence : { actorMargin: 50 }
    });
  }, window.mermaid);
}
</script>


  

  

  


<script>
NexT.utils.loadComments(document.querySelector('#valine-comments'), () => {
  NexT.utils.getScript('//unpkg.com/valine/dist/Valine.min.js', () => {
    var GUEST = ['nick', 'mail', 'link'];
    var guest = 'nick,mail';
    guest = guest.split(',').filter(item => {
      return GUEST.includes(item);
    });
    new Valine({
      el         : '#valine-comments',
      verify     : false,
      notify     : false,
      appId      : 'szhBf0Qamzsowubi1WnkXmUj-gzGzoHsz',
      appKey     : 'WpwxoK0fVJHvhzWwakr9vbpA',
      placeholder: "Just go go",
      avatar     : 'mm',
      meta       : guest,
      pageSize   : '10' || 10,
      visitor    : false,
      lang       : '' || 'zh-cn',
      path       : location.pathname,
      recordIP   : false,
      serverURLs : ''
    });
  }, window.Valine);
});
</script>

</body>
</html>
